package com.example.demo.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * Description:
 * User: 故我
 * Date: 2023-11-07
 * Time: 10:25
 */
@Service
public class LogService {

    //使用默认的加入事务
    @Transactional(propagation = Propagation.NESTED)
    public int add() {

        try {
            int result = 10 / 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return 1;
    }
}
